package com.android.equipment.dao.operate;

import com.android.equipment.ObjectBox;
import com.android.equipment.dao.Data;
import com.android.equipment.dao.Data_;
import com.blankj.utilcode.util.TimeUtils;

import java.util.List;

import io.objectbox.Box;
import io.objectbox.query.QueryBuilder;

public class DaoData {
    static Box<Data> dataBox;
    private static DaoData instance;

    private DaoData() {
    }

    public static DaoData getInstance() {
        if (instance == null) {
            instance = new DaoData();
            dataBox = ObjectBox.get().boxFor(Data.class);
        }
        return instance;
    }

    public long addData(Data data) {
        return dataBox.put(data);
    }

    public void delData(Data data) {
        dataBox.remove(data);
    }

    public void delDao(List<Data> dataList){
        dataBox.remove(dataList);
    }


    //根据设备号 和 时间倒序查询出当前设备最新推送过来的数据
    public Data getDataByDeviceCode(String deviceCode) {
        return dataBox.query()
                .equal(Data_.device_code, deviceCode, QueryBuilder.StringOrder.CASE_INSENSITIVE)
                .orderDesc(Data_.create_time)
                .build().findUnique();
    }

    public Data getDataById(long id){
        return dataBox.get(id);
    }

    public void deleteAllData(){
        dataBox.removeAll();
    }

    public void deleteDataByDeviceCode(String deviceCode){
        dataBox.remove(dataBox.query()
                .equal(Data_.device_code, deviceCode, QueryBuilder.StringOrder.CASE_INSENSITIVE)
                .build().find());
    }

    public List<Data> getAllDataByDeviceCode(String deviceCode){
        return dataBox.query()
                .equal(Data_.device_code, deviceCode, QueryBuilder.StringOrder.CASE_INSENSITIVE)
                .build().find();
    }


    public Long getDataCountByDeviceCode(String deviceCode){
        return dataBox.query()
                .equal(Data_.device_code, deviceCode, QueryBuilder.StringOrder.CASE_INSENSITIVE)
                .build().count();
    }
    public List<Data> getDataByDeviceCodeAndPage(String deviceCode, int page){
        return dataBox.query()
                .equal(Data_.device_code, deviceCode, QueryBuilder.StringOrder.CASE_INSENSITIVE)
                .orderDesc(Data_.create_time)
                .build().find(page, 10);
    }

    public Data getLastTimeDataByDeviceCode(String deviceCode){
        return dataBox.query()
                .equal(Data_.device_code, deviceCode, QueryBuilder.StringOrder.CASE_INSENSITIVE)
                .orderDesc(Data_.create_time)
                .build().findFirst();
    }


    public Data getProgressDataByDeviceCode(String deviceCode, long beginTime){
        return dataBox.query()
                .equal(Data_.device_code, deviceCode, QueryBuilder.StringOrder.CASE_INSENSITIVE)
                .greater(Data_.create_time,beginTime)
                .orderDesc(Data_.create_time)
                .build().findFirst();
    }

    public Data getCompletedDataByDeviceCode(String deviceCode, long beginTime, long endTime){
        return dataBox.query()
                .equal(Data_.device_code, deviceCode, QueryBuilder.StringOrder.CASE_INSENSITIVE)
                .greater(Data_.create_time,beginTime)
                .less(Data_.create_time,endTime)
                .orderDesc(Data_.create_time)
                .build().findFirst();
    }


}
